package cn.kxx.controller;

import cn.kxx.pojo.Permission;
import cn.kxx.pojo.Role;
import cn.kxx.service.RoleServise;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: Kong Xiang Xiang
 * @Date: 2022/11/6 17:20
 * @DESC:
 */
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleServise roleServise;


    /**
     * @描述: 查询所有角色
     * @参数:
     * @返回值:
     * @创建人: kxx
     * @创建时间: 2022/11/6 17:21
     * @修改人和其它信息:
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "5") Integer size) {

        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(page, size);
        List<Role> list = roleServise.findAll();
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageInfo", pageInfo);

        mv.setViewName("role-list");

        return mv;
    }

    //  查询角色详情
    @RequestMapping("findById")
    public ModelAndView findById(Integer id) {

        ModelAndView mv = new ModelAndView();
        Role role = roleServise.findById(id);
        mv.addObject("role",role);
        mv.setViewName("role-show");
        return mv;


    }

    //新增角色
    @RequestMapping("save")
    public String save(Role role) {


        roleServise.save(role);


        return "redirect:/role/findAll.do";


    }

    //新增角色的权限 strat
    @RequestMapping("findRoleByIdAndAllPermission")
    public String findRoleByIdAndAllPermission(Integer id ,HttpServletRequest request) {

        HttpSession session = request.getSession();
       session.setAttribute("roleId",id);


        List<Permission> permissionList =  roleServise.findRoleByIdAndAllPermission(id);

        session.setAttribute("permissionList",permissionList);

        return "redirect:/pages/role-permission-add.jsp";


    }
    @RequestMapping("addPermissionToRole")
    public String addPermissionToRole(Integer roleId,int[] ids) {

        roleServise.addPermissionToRole(roleId,ids);

        return "redirect:/role/findAll.do";


    }

    //新增角色的权限 end

}
